﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HYBYSoft.Company.Models;

namespace HYBYSoft.Company.WebSite.Controllers
{
    public class MessageController : Controller
    {
        /// <summary>
        /// 数据库实体上下文对象
        /// </summary>
        CompanySiteEntities _dbContext = new CompanySiteEntities();

        /// <summary>
        /// 留言列表首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            //查询出所有未被删除的留言（即状态为1）
            var message = from m in _dbContext.QuestionMessages
                          where m.State == 1
                          select new QuestionAndReplyMessages
                          {
                              QuestionID = m.QuestionID,
                              CreateTime = m.CreateTime,
                              State = m.State,
                              IsReply = m.IsReply,
                              LastReplyTime = m.LastReplyTime,
                              Titles = m.Titles,
                          };

            return View(message);
        }

        /// <summary>
        /// 留言详细查看页面
        /// </summary>
        /// <param name="id">问题编号</param>
        /// <returns></returns>
        public ActionResult Details(int id)
        {
            //查询留言的详细内容
            var message = (from m in _dbContext.QuestionMessages
                           join reply in _dbContext.ReplyMessages on m.QuestionID equals reply.QuestionID into replyMessage
                           where m.QuestionID == id && m.State == 1
                           orderby m.CreateTime descending
                           select new QuestionAndReplyMessages
                           {
                               QuestionID = m.QuestionID,
                               CreateTime = m.CreateTime,
                               State = m.State,
                               IsReply = m.IsReply,
                               LastReplyTime = m.LastReplyTime,
                               Titles = m.Titles,
                               ReplyMessages = replyMessage
                           }).First();

            return View(message);
        }


        /// <summary>
        /// 新增留言Get请求
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// 新增留言
        /// </summary>
        /// <param name="message">问题消息实体</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(QuestionMessage message)
        {
            QuestionMessage questionMessage = new QuestionMessage();
            questionMessage.Titles = message.Titles;
            questionMessage.CreateTime = DateTime.Now;
            questionMessage.LastReplyTime = DateTime.Now;
            questionMessage.State = 1;
            questionMessage.IsReply = 0;

            _dbContext.QuestionMessages.AddObject(questionMessage);
            _dbContext.SaveChanges();

            return RedirectToAction("Index");
        }

        /// <summary>
        /// 回复留言
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Reply()
        {
            int questionID = int.Parse(Request["QuestionID"]);
            var questionMessage = _dbContext.QuestionMessages.FirstOrDefault(q => q.QuestionID == questionID);

            //if (questionMessage.IsReply == 1)
            //{
                ReplyMessage replyMessage = new ReplyMessage();

                replyMessage.QuestionID = questionID;
                replyMessage.Contents = Request["replyContents"];
                replyMessage.CreateTime = DateTime.Now;
                replyMessage.IsAdmin = 0;
                //将该QuestionID（问题）下的回复状态修改为0（即：未回复）            
                questionMessage.IsReply = 0;

                _dbContext.ReplyMessages.AddObject(replyMessage);
                _dbContext.SaveChanges();

                return RedirectToAction("Details", new { id = replyMessage.QuestionID });
            //}

            //return View();
        }


    }
}
